package notes.java.concurrent;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 模拟 等待唤醒
 * @author wangcs
 *
 */
public class U12_TestReentrantLock2 {

	public static void main(String[] args) {
		Lock reentrantLock = new ReentrantLock();
		Condition condition = reentrantLock.newCondition();
		// 线程一获取信号，只有获取到信号才能再执行
		new Thread(()->{
			reentrantLock.lock();
			System.out.println("thread 1 ready...");
			// 等待唤醒
			try {
				condition.await();
				System.out.println("thread1 run...");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			reentrantLock.unlock();
		}).start();
		
		new Thread(()->{
			reentrantLock.lock();
			try {
				System.out.println("thread2 ready...");
				Thread.sleep(5*1000);
				// 唤醒所有
				condition.signalAll();
				System.out.println("thread2 singnalAll...");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			reentrantLock.unlock();
		}).start();
	}
}
